import pygame, os, time
from pygame.locals import *

import ship

class PlayerShip(ship.Ship):
    def __init__(self, x, y, xlimit, ylimit, filename):
        self.SPEEDCHANGE = 10
        self.filename = os.path.join("img", filename)
        self.surface = pygame.image.load(self.filename) # SOooo, if the fiel dasent exists it's no biggie. just be INVISIBLE SHIP MAN!!!!!!!!!!

        self.x = x
        self.y = y
        self.xspeed = 0
        self.yspeed = 0
        self.xlimit = xlimit-self.surface.get_width()
        self.ylimit = ylimit-self.surface.get_height()
        self.hp = 3
        self.invul = False
        self.invultime = time.time()
    
    def update(self):
        self.x += self.xspeed
        self.y += self.yspeed

        if self.y > self.ylimit:
            self.y = self.ylimit
        elif self.y < 0:
            self.y = 0
        if self.x > self.xlimit:
            self.x = self.xlimit
        elif self.x < 0:
            self.x = 0

        if self.invul:
            if self.invultime < time.time():
                self.invul = False

        return True

    def eventKeyDown(self, key):
        if key == K_LEFT:
            self.xspeed -= self.SPEEDCHANGE
        if key == K_RIGHT:
            self.xspeed += self.SPEEDCHANGE
        if key == K_UP:
            self.yspeed -= self.SPEEDCHANGE
        if key == K_DOWN:
            self.yspeed += self.SPEEDCHANGE

    def eventKeyUp(self, key):
        if key == K_LEFT:
            self.xspeed += self.SPEEDCHANGE
        if key == K_RIGHT:
            self.xspeed -= self.SPEEDCHANGE
        if key == K_UP:
            self.yspeed += self.SPEEDCHANGE
        if key == K_DOWN:
            self.yspeed -= self.SPEEDCHANGE

    def hit(self):
        if self.invul == False:
            self.invul = True
            self.hp -= 1
            self.invultime = time.time()+2
            print "hit, inte invul"
        print "hit, kanske invul"
